HAL LPTIM initialization and configuration functions migration

The goal of this page is to show how to migrate the initialization sequence using STM32CubeMX2:

  • Open STM32CubeMX2 LPTIM configuration panel.

  • Configure the SW layer HAL.

  • Configure the LPTIM main features.

  • Configure the LPTIM advanced features.

  • Configure the LPTIM GPIO pins.

  • Configure the LPTIM DMA(s) if the application requires to use the LPTIM in DMA mode.

  • Configure the NVIC if the application requires the LPTIM to be used in IT mode.

  • Generate the code.

HAL LPTIM configuration panel

Create a new project with the STM32CubeMX2, then start the initialization and the main configuration.

Project configuration panel

HAL LPTIM main features configuration

Configure the LPTIM main features.

LPTIM main features configuration

HAL LPTIM advanced features configuration

Configure the LPTIM advanced features.

LPTIM advanced features configuration

GPIO configuration

Configure GPIO pins.

LPTIM GPIO configuration

DMA configuration

Configure the DMA for data reception as needed.

LPTIM DMA configuration

NVIC configuration

Configure the NVIC in case of an interrupt.

LPTIM NVIC configuration

Generated LPTIM initialization and de-initialization sequences

The LPTIM initialization sequence is generated in the mx_lptim1_init function in mx_lptim1.c file under $YOUR_PROJECT_NAME$/$YOUR_PROJECT_NAME$_SW/generated/STM32Cube_CodeGen.

Related concepts:

STM32CubeMX2 Generated : LPTIM initialization and de-initialization sequences

Topic

Code Snippet HAL1

Code Snippet HAL2

Handle declaration

LPTIM_HandleTypeDef hlptim1;
static hal_lptim_handle_t hLPTIM1;

Initialization and configuration sequence

static void MX_LPTIM1_Init(void)
{

  LPTIM_OC_ConfigTypeDef sConfig1 = {0};

  hlptim1.Instance = LPTIM1;
  hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC;
  hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL;
  hlptim1.Init.Period = 65535;
  hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1;
  hlptim1.Init.RepetitionCounter = 0;









  if (HAL_LPTIM_Init(&hlptim1) != HAL_OK)
  {
    Error_Handler();
  }






  sConfig1.Pulse = 1;
  sConfig1.OCPolarity = LPTIM_OCPOLARITY_HIGH;

  if (HAL_LPTIM_OC_ConfigChannel(&hlptim1, &sConfig1, LPTIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
}

void HAL_LPTIM_MspInit(LPTIM_HandleTypeDef* hlptim)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
 /* LPTIM Clock enable */
  RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  if(hlptim->Instance==LPTIM1)
  {
    PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_LPTIM1;
    PeriphClkInit.Lptim1ClockSelection = RCC_LPTIM1CLKSOURCE_MSIK;
    if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
    {
      Error_Handler();
    }

    /* Peripheral clock enable */
    __HAL_RCC_LPTIM1_CLK_ENABLE();
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_2;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_LPTIM1;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    /* LPTIM1 interrupt Init */
    HAL_NVIC_SetPriority(LPTIM1_IRQn, 0, 0);
    HAL_NVIC_EnableIRQ(LPTIM1_IRQn);
  }
}
hal_lptim_handle_t *mx_lptim1_init(void)
{
  hal_lptim_config_t  config;
  hal_lptim_oc_config_t oc_config1;

  /* Timer configuration to reach the output frequency at 88 Hz */
  config.clock_source       = HAL_LPTIM_CLK_INTERNAL;

  config.period             = 0xFFFF;
  config.prescaler          = HAL_LPTIM_CLK_SRC_DIV1;
  config.repetition_counter = 0;
  config.mode               = HAL_LPTIM_CONTINUOUS; /* to generate a PWM signal */

  /* LPTIM Clock Enable */
  HAL_RCC_LPTIM1_EnableClock();
  if (HAL_RCC_LPTIM1_SetKernelClkSource(HAL_RCC_LPTIM1_CLK_SRC_PCLK3) != HAL_OK)
  {
    return NULL;
  }

  if (HAL_LPTIM_Init(&hLPTIM1, HAL_LPTIM1) != HAL_OK)
  {
    return NULL;
  }

  if (HAL_LPTIM_SetConfig(&hLPTIM1, &config) != HAL_OK)
  {
    return NULL;
  }

  oc_config1.pulse = 1;
  oc_config1.polarity = HAL_LPTIM_OC_HIGH;

  if (HAL_LPTIM_OC_SetConfigChannel(&hLPTIM1, HAL_LPTIM_CHANNEL_1, &oc_config1) != HAL_OK)
  {
    return NULL;
  }


  /* GPIO configuration for channel1 */


  hal_gpio_config_t  gpio_config;









 /*GPIO Clock Enable*/

  HAL_RCC_GPIOB_EnableClock();


  gpio_config.mode        = HAL_GPIO_MODE_ALTERNATE;
  gpio_config.output_type = HAL_GPIO_OUTPUT_PUSHPULL;
  gpio_config.pull        = HAL_GPIO_PULL_NO;
  gpio_config.speed       = HAL_GPIO_SPEED_FREQ_LOW;
  gpio_config.alternate   = HAL_GPIO_AF_1;
  HAL_GPIO_Init(HAL_GPIOB, HAL_GPIO_PIN_2, &gpio_config);


  /* Enable the interruption for LPTIM */
  HAL_CORTEX_NVIC_SetPriority(LPTIM1_IRQn, HAL_CORTEX_NVIC_PREEMP_PRIORITY_0, HAL_CORTEX_NVIC_SUB_PRIORITY_0);
  HAL_CORTEX_NVIC_EnableIRQ(LPTIM1_IRQn);


  return &hLPTIM1;
}

Deinitialization sequence

static void MX_LPTIM1_DeInit(void)
{
 HAL_LPTIM_DeInit(&hlptim);
}

void HAL_LPTIM_MspDeInit(LPTIM_HandleTypeDef* hlptim)
{
  if(hlptim->Instance==LPTIM1)
  {
    __HAL_RCC_LPTIM1_CLK_DISABLE();



/* De-initialize all GPIO pins associated with LPTIM1 */
    HAL_GPIO_DeInit(GPIOB, GPIO_PIN_2);

    /* LPTIM1 interrupt DeInit */
    HAL_NVIC_DisableIRQ(LPTIM1_IRQn);

  }

}
void mx_lptim1_deinit(void)
{
  (void)HAL_LPTIM_DeInit(&hLPTIM1);





  HAL_RCC_LPTIM1_Reset();
  HAL_RCC_LPTIM1_DisableClock();



  /* De-initialize all GPIO pins associated with LPTIM1 */
  HAL_GPIO_DeInit(HAL_GPIOB, HAL_GPIO_PIN_2);

  /* LPTIM1 interrupt DeInit */
  HAL_CORTEX_NVIC_DisableIRQ(LPTIM1_IRQn);
}

IRQ handle function

void LPTIM1_IRQHandler(void)
{
  HAL_LPTIM_IRQHandler(&LptimHandle);
}
void LPTIM1_IRQHandler(void)
{
  HAL_LPTIM_IRQHandler(&LptimHandle);
}